package net.zjitc.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5WithSaltUtil {


    // 计算带盐值的MD5哈希
    public static String hashPasswordWithSalt(String password, String salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(salt.getBytes());
            byte[] messageDigest = md.digest(password.getBytes());
            return bytesToHex(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    // 将字节数组转换为十六进制字符串
    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }

    // 加密密码
    public static String hashPasswordSalt(String password, String salt) {
        String hashedPassword = hashPasswordWithSalt(password, salt);
        return hashedPassword; // 返回盐值和哈希值
    }


}